<template>
  <div class="item">
    <el-row class="header" >
      <el-col :span="6">
        {{ list.partMaster && list.partMaster.partCode }}
      </el-col>
      <el-col :span="10">
        {{ list.partMaster && list.partMaster.partName }}
      </el-col>
      <el-col :span="6">
        {{ list.quantity }}
      </el-col>
      <el-col :span="2">
        <div v-if="list.child && list.child.length > 0" class="block" @click="toggleExpand">
          <el-tooltip effect="dark" :content="expand ? '收起' : '展开'" placement="top">
            <span v-if="!expand"><i class="el-icon-plus" /></span>
            <span v-else><i class="el-icon-minus" /></span>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  name: 'NodeItem',
  inject: ['getNode', 'getGraph'],
  data() {
    return {
      imgSrc: '',
      srcList: [],
      list: {},
      expand: false
    }
  },
  created() {
    const node = this.getNode()
    this.list = node.getData()
    this.expand = this.list.expand // 手动设置展开状态
  },
  mounted() {
    const node = this.getNode()
    const graph = this.getGraph()
    node.on('change:data', ({ current }) => {
      this.expand = current.expand // 改变图标显示状态
    })
    // 监听可见事件-当节点隐藏的时候，遍历子节点并隐藏，隐藏所有后代子节点
    node.on('change:visible', () => {
      // 当存在子节点的时候，遍历子节点并显示
      if (node.data.child) {
        node.data.child.forEach(key => {
          const child = graph.getCellById(key) // 显示链接的子节点
          const expandChild = graph.getCellById(`expand-${key}`) // 子节点的收缩节点（不一定存在）
          if (node.visible && node.data.expand) {
            child.show() // 显示子节点
            expandChild?.show() // 存在收缩节点也显示
          } else {
            child.hide() // 隐藏子节点
            expandChild?.hide() // 子节点存在收缩节点隐藏
          }
        })
      }
    })
  },
  methods: {
    // btnClick(data) {
    //   // console.log(type, data)
    //   // this.list.child = []/
    //   // 触发自定义事件，传递一些数据
    //   // console.log(data, 'data')
    //   this.$emit('myCustomEvent', data)
    // },
    // 当前节点child-下一级显示隐藏，只控制子节点
    toggleExpand() {
      const node = this.getNode()
      const graph = this.getGraph()
      // 遍历子节点及其收缩节点进行 显示/隐藏
      if (node.data.child) {
        node.data.child.forEach(key => {
          const child = graph.getCellById(key)
          const expandChild = graph.getCellById(`expand-${key}`)
          if (this.expand) {
            child.hide()
            expandChild?.hide()
          } else {
            child.show()
            expandChild?.show()
          }
        })
      }
      // 更新收缩情况
      node.setData({
        expand: !this.expand
      })
    }
  }
}

</script>

<style scoped lang="scss">
* {
  font-size: 12px
}

$row-1: 28px;
$bor-color: #D3D5D6;

.ellipsis {
  white-space: nowrap;
  /* 防止文本换行 */
  overflow: hidden;
  /* 隐藏溢出的文本 */
  text-overflow: ellipsis;
  /* 使用省略号表示被截断的文本 */
  width: 100%;
  /* 设置一个宽度，以便看到效果 */
}

.ellipsis2 {
  // white-space: nowrap; /* 防止文本换行 */
  overflow: hidden;
  /* 隐藏溢出的文本 */
  display: -webkit-box;
  width: 100%;
  /* 设置一个宽度，以便看到效果 */
}

.text-center {
  text-align: center;
}

.bor-top {
  border-top: 1px solid $bor-color;
}

.bor-left {
  border-left: 1px solid $bor-color;
}

.h53 {
  height: 53px;
  overflow: hidden;
}

.pl-16 {
  padding-left: 16px;
}

.item {
  border: 1px solid $bor-color;
  background: #fff;
  color: #3D3D3D;
  line-height: $row-1;
  position: relative;

  .header {
    background: #F7F8FA;
    height: $row-1;
    line-height: $row-1;

    .el-col {
      border-left: 1px solid $bor-color;
      text-align: center;

      &:first-child {
        border: none;
      }
    }
  }

  $h: 115px;

  .content {
    border: 1px solid $bor-color;
    border-width: 1px 0;

    .content-left {
      text-align: center;
      height: $h;
      line-height: $h;

      .el-image {
        max-height: $h;
      }
    }

    .content-right {
      height: $h;
      border-left: 1px solid $bor-color;

      .el-col {
        line-height: $row-1;
      }
    }
  }

  .footer {
    height: 56px;
    line-height: 56px;
    padding: 0 16px;
  }

  .block {
    width: 28px;
    height: 28px;
    text-align: center;
    background-color: #ccc;
    // border: 1px solid $bor-color;
    position: absolute;
    right: 0;
    top: 0;
  }
}

.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
